//
//  StickyController.m
//  StickyDemo
//
//  Created by 胡不弍 on 2021/7/2.
//

#import "StickyController.h"
#import "TabController.h"
#import "CustomScrollView.h"

// 导航栏高度
#define NavBarHeight 44.0

// 头部宏
#define SafeAreaTopHeight (StatusBarHeight + NavBarHeight)
// 底部宏
#define SafeAreaBottomHeight 34

/** 屏幕宽 */
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
/** 屏幕高 */
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface StickyController ()<UIScrollViewDelegate>

@property (nonatomic, strong) NSArray *categorys;
@property (nonatomic, assign) BOOL isEndDragging;
@property (nonatomic, assign) CGRect OriginFrame;

@property (nonatomic, strong) CustomScrollView *topScrollView;

@end

@implementation StickyController

- (instancetype)init {
    if (self = [super init]) {
        self.menuViewStyle = WMMenuViewStyleLine;
        self.progressColor = [UIColor redColor];
        self.titleColorSelected = [UIColor blueColor];
        self.titleColorNormal = [UIColor systemGrayColor];
        self.titleFontName = @"PingFangSC-Regular";
        self.titleSizeSelected = 16;
        self.titleSizeNormal = 15;
        self.progressWidth = 35;
        self.progressViewCornerRadius = 1;
        self.progressHeight = 2;
        self.menuItemWidth = 120;
        self.menuViewHeight = 50;
        
        self.minimumHeaderViewHeight = 100;
        self.maximumHeaderViewHeight = 300;
    }
    return self;
}

- (void)viewDidLoad {
    UIView *headerView = [UIView new];
    headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 300);
    headerView.backgroundColor = [UIColor redColor];
    [self.view addSubview:headerView];
    [self.scrollView setContentSize:CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT + 100)];
    
    [self reloadData];
    
    _topScrollView = [[CustomScrollView alloc] init];
    _topScrollView.frame = self.view.bounds;
    _topScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT + 300);
    _topScrollView.delegate = self;
    if (@available(iOS 11.0, *)) {
        _topScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [_topScrollView setBounces:YES];
    [self.view insertSubview:_topScrollView aboveSubview:self.view];
    _topScrollView.tag = 1900;
}

- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return self.categorys.count;
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
    TabController *tabController = [TabController new];
    if (index == 0) {
        [tabController.view setBackgroundColor:[UIColor greenColor]];
    } else if (index == 1) {
        [tabController.view setBackgroundColor:[UIColor purpleColor]];
    } else {
        [tabController.view setBackgroundColor:[UIColor yellowColor]];
    }
    return tabController;
}

- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    return self.categorys[index];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    if (scrollView.tag == 1900) {
        self.OriginFrame = self.view.frame;
        return;
    }
    [super scrollViewWillBeginDragging:scrollView];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (scrollView.tag == 1900) {
        CGFloat y = scrollView.contentOffset.y;
        if (y > 0) {
            return;
        }
        CGRect frame = self.view.frame;
        CGFloat offY = self.OriginFrame.origin.y - frame.origin.y;
        if (offY < -10) {
            if (self.didDownREfresh) {
                self.didDownREfresh();
            }
        } else {
            if (self.unchangedScroll) {
                self.unchangedScroll();
            }
        }
        return;
    }
    
    [super scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.tag == 1900) {
        CGFloat y = scrollView.contentOffset.y;
        
        NSLog(@"- %f",y);
        CGRect frame = self.view.frame;
        if (y > 0 && self.OriginFrame.origin.y >= frame.origin.y) {
            if (self.didUpScroll) {
                self.didUpScroll(y);
            }
            return;
        }
        frame.origin.y -= y;
        self.view.frame = frame;
        CGFloat offY = self.OriginFrame.origin.y - frame.origin.y;

        if (self.didDownScroll) {
            self.didDownScroll(offY);
        }
    }
    [super scrollViewDidScroll:scrollView];
}
#pragma mark - LazyLoad
- (NSArray *)categorys {
    if (!_categorys) {
        _categorys = [[NSArray alloc] init];
        _categorys = @[@"单曲", @"专辑", @"视频"];
    }
    return _categorys;
}
@end
